/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.qudexapps.xmllib.utilities;

import java.util.regex.Pattern;

import uk.ljmu.metadata.utilities.MetadataUtilities;
import uk.ljmu.metadata.utilities.MetadataUtilities.ValueType;
import uk.ljmu.qudexapps.xmllib.services.CsvManagementImpl;
import uk.ljmu.qudexapps.xmllib.services.ExcelManagementImpl;
import uk.ljmu.qudexapps.xmllib.services.SpreadsheetManagement;

public class SpreadsheetUtilities 
{
	/**
	 * Static class to hold a Column structure representing a Column in the spreadsheet
	 * @author agustinamartinez
	 *
	 */
	public static class Column 
	{
		/**
		 * Spreadsheet header column name
		 */
		private String columnName;
		/**
		 * Spreadsheet header colum type
		 */
		private ValueType columnType = ValueType.Text;
		/**
		 * Spreadsheet header column index
		 */
		private Integer	columnIndex = -1;
		/**
		 * @return the columnName
		 */
		public String getColumnName() {
			return columnName;
		}
		/**
		 * @param columnName the columnName to set
		 */
		public void setColumnName(String columnName) {
			this.columnName = columnName;
		}
		/**
		 * @return the columnType
		 */
		public ValueType getColumnType() {
			return columnType;
		}
		/**
		 * @param columnType the columnType to set
		 */
		public void setColumnType(ValueType columnType) {
			this.columnType = columnType;
		}
		/**
		 * @return the columnIndex
		 */
		public Integer getColumnIndex() {
			return columnIndex;
		}
		/**
		 * @param columnIndex the columnIndex to set
		 */
		public void setColumnIndex(Integer columnIndex) {
			this.columnIndex = columnIndex;
		}
	}
	
	/** 
	 *  elapsed time in hours/minutes/seconds 
	 * @return String 
	 */  
	public static String getElapsedTimeHoursMinutesSecondsString(long elapsedTime) {       

		String format = String.format("%%0%dd", 2);  
		elapsedTime = elapsedTime / 1000;  
		String seconds = String.format(format, elapsedTime % 60);  
		String minutes = String.format(format, (elapsedTime % 3600) / 60);  
		String hours = String.format(format, elapsedTime / 3600);  
		String time =  hours + ":" + minutes + ":" + seconds;  
		return time;  
	}
	/**
	 * Returns the appropriate manager based on the working type spreadsheet
	 * @param fileType string containing the file mimetype
	 * @return ExcelManagement. CSVManagement or NULL
	 */
	public static SpreadsheetManagement getSpreadsheetManager(String fileType, boolean isQudexSS, boolean isQudexCollection) {
		if (fileType.equals(MetadataUtilities.MIMETYPES_MAP.get("excel"))
				|| fileType.equals(MetadataUtilities.MIMETYPES_MAP.get("excel2"))
				|| fileType.equals(MetadataUtilities.MIMETYPES_MAP.get("excel3"))
				|| fileType.equals(MetadataUtilities.MIMETYPES_MAP.get("excel4"))
				|| fileType.equals(MetadataUtilities.MIMETYPES_MAP.get("excel5"))
				|| fileType.equals(MetadataUtilities.MIMETYPES_MAP.get("excel6"))) {
			return new ExcelManagementImpl(isQudexSS, isQudexCollection);
		} else if (fileType.equals(MetadataUtilities.MIMETYPES_MAP.get("csv"))
				|| fileType.equals(MetadataUtilities.MIMETYPES_MAP.get("csv2")))
			return new CsvManagementImpl(isQudexSS, isQudexCollection);
		//AMG- DEFAULT Excel Manager
		return new ExcelManagementImpl(isQudexSS, isQudexCollection);
	}
	
	 public static int compareVersions(String v1, String v2) {
	        String s1 = normalisedVersion(v1);
	        String s2 = normalisedVersion(v2);
	        int cmp = s1.compareTo(s2);
	        
	        return cmp;
	    }

	    public static String normalisedVersion(String version) {
	        return normalisedVersion(version, ".", Constants.VERSION_MAX_DIGITS);
	    }

	    public static String normalisedVersion(String version, String sep, int maxWidth) {
	        String[] split = Pattern.compile(sep, Pattern.LITERAL).split(version);
	        StringBuilder sb = new StringBuilder();
	        for (String s : split) {
	            sb.append(String.format("%" + maxWidth + 's', s));
	        }
	        return sb.toString();
	    }

}
